package com.fwj.user.web.rbac;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.user.api.rbac.RBACMenuService;
import com.fwj.user.entity.rbac.menu.Menu;
import com.fwj.user.entity.user.UserBaseInfo;
import com.fwj.user.model.rbac.menu.MenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: fwj
 * @Data: 2022/5/1 17:29
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/RBACMenu")
public class RBACMenuController {


    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(RBACMenuController.class);

    @Autowired
    private RBACMenuService menuService;

    /**
     * @Description TODO 保存菜单，添加或修改
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/saveMenu")
    public JSONObject saveMenu(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            MenuVo menuVo = JSONObject.parseObject(jsonObject.toJSONString(),MenuVo.class);
            Menu menu = buildMenu(menuVo,userBaseInfo);
            return menuService.saveMenu(menu);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    private Menu buildMenu(MenuVo menuVo, UserBaseInfo userBaseInfo) {
        Menu menu = new Menu();
        menu.setTitle(menuVo.getTitle());
        menu.setCreator(userBaseInfo.getId());
        return menu;
    }

    /**
     * @Description TODO 删除菜单
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/deleteMenu")
    public JSONObject deleteMenu(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            String menuNumber = jsonObject.getString("menuNumber");
            return menuService.deleteMenu(menuNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


    /**
     * @Description TODO 查询菜单
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryMenu")
    public JSONObject queryMenu(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            return menuService.queryMenu(jsonObject);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }



}
